package org.shilong.MLogic.WebRequest;

import java.io.IOException;
import java.net.ConnectException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.shilong.Utility.constants.URLConstants;

/**
 * 访问网络--创建名片
 * @author Administrator
 *
 */
public class CreateOrUpdateBussinessCardHttpLogic {
	

	private static String rlt = null;

	private static HttpClient httpClient = null;

	private static List<NameValuePair> params = null;

	private static HttpPost request = null;

	private static HttpResponse response = null;
	
	
	/**
	 * 访问网络
	 * @param args
	 * @return
	 */
	public static String getRlt( String... args ){
		
		String url = "";
		
		if( args[7].equals("create"))url = URLConstants.URL_CREATE_BUSINESS_CARD;
		if( args[7].equals("alert"))url = URLConstants.URL_ALERT_BUSINESS_CARD;
		
		rlt = null;

		httpClient = null;

		response = null;

		params = null;

		params = new ArrayList<NameValuePair>();

		params.add( new BasicNameValuePair( "a_name", args[0] ) );	//	真实姓名
		params.add( new BasicNameValuePair( "telnum", args[1] ) );	//	号码
		params.add( new BasicNameValuePair( "com_name", args[2] ) );	//	公司名
		params.add( new BasicNameValuePair( "buss", args[3] ) );	//	行业
		params.add( new BasicNameValuePair( "domain_content", args[4] ) );	//	领域
		params.add( new BasicNameValuePair( "usex", args[5] ) );	//	 性别
		params.add( new BasicNameValuePair( "uage", args[6] ) );	//	年龄
		
		request = new HttpPost( url );  //  创建名片地址

		try {
			request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

			httpClient = new DefaultHttpClient();

			response = httpClient.execute(request);

			if (response.getStatusLine().getStatusCode() == 200) {

				rlt = EntityUtils.toString(response.getEntity());
			}

		}catch (HttpHostConnectException ex) {

			rlt = "neterror";

			request.abort();

		}catch (ConnectException ex) {

			rlt = "neterror";

			request.abort();

		}catch (ClientProtocolException e) {

			rlt = "protocloerror";

			request.abort();
		}catch (IOException e) {

			rlt = "ioerror";

			request.abort();
		}  

		return rlt;
	}
	
}
